import { ProForm, ProFormCascader } from '@ant-design/pro-form'
import { request } from '@designable/event-system'
import React from 'react'

// 租户字典管理
function tenantDicGet(params: { code: string }) {
  return request({
    uri: '/api/tenant/dic/get',
    params,
    method: 'GET',
  })
}

export function FormCascaderCategory() {
  return (
    <ProForm.Item noStyle shouldUpdate>
      {(form) => {
        return (
          <>
            <ProFormCascader
              noStyle
              name={'categoryTypes'}
              fieldProps={{
                onChange: (value: any) => {
                  if (Array.isArray(value)) {
                    form.setFieldsValue({
                      categoryType: value[0],
                      subType: value[1],
                    })
                  } else {
                    form.setFieldsValue({
                      categoryType: '',
                      subType: '',
                    })
                  }
                },
              }}
              request={async () => {
                const res = (await tenantDicGet({
                  code: 'GlobalCategoryTypeTree',
                })) as any
                return res
              }}
            ></ProFormCascader>
            <ProForm.Item name={'categoryType'} hidden></ProForm.Item>
            <ProForm.Item name={'subType'} hidden></ProForm.Item>
          </>
        )
      }}
    </ProForm.Item>
  )
}
